package koction.controller;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import koction.model.admin.AdminService;
import koction.model.admin.AdminVO;
import koction.model.admin.AskSellerVO;
import koction.model.consumer.ConsumerService;
import koction.model.consumer.ConsumerVO;
import koction.model.product.ProductVO;
import koction.model.question.QuestionBoardService;
import koction.model.question.QuestionBoardVO;
import koction.model.seller.SellerService;
import koction.model.seller.SellerVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdminController {
	@Resource(name="adminServiceImpl")
	private AdminService adminService;
	@Resource(name="consumerServiceImpl")
	private ConsumerService consumerService;
	@Resource(name="questionBoardServiceImpl")
	private QuestionBoardService questionBoardService;
	@Resource(name="sellerServiceImpl")
	private SellerService sellerService;
	
	@RequestMapping("/admin_login.do")
	public String adminLoginView(HttpSession session){
		AdminVO avo=(AdminVO) session.getAttribute("avo");
		String path="";
		if(avo!=null){
			path="adminHome";
		}else{
			path="adminLoginView.adminView";
		}
		return path;
	}	
	@RequestMapping("/adminLogin.do")
	public ModelAndView adminLogin(AdminVO avo,HttpSession session){
		AdminVO vo=adminService.adminLogin(avo);
		String path="";
		if(vo==null){
			path="loginAdminFail.admin";
		} else {
			session.setAttribute("avo", vo);
			path="adminHome";
		}
		return new ModelAndView(path);
	}
	@RequestMapping("/responseSellerView.do")
	public ModelAndView responseSellerView(){
		List<AskSellerVO> list=adminService.responseSellerView();
		return new ModelAndView("responseSellerView.admin","list",list);
	}
	@RequestMapping("/adminLogout.do")
	public String adminLogout(HttpSession session){
		session.invalidate();
		return "home";
	}
	@RequestMapping("/responseSeller.do")
	@ResponseBody
	public List<AskSellerVO> responceSeller(AskSellerVO asvo){
		System.out.println(asvo.toString());
		adminService.responceSeller(asvo);
		List<AskSellerVO> list=adminService.responseSellerView();
		return list;
	}
	
	@RequestMapping("/responseSellerCancle.do")
	@ResponseBody
	public List<AskSellerVO> responseSellerCancle(AskSellerVO asvo){
		adminService.responseSellerCancle(asvo.getId());
		List<AskSellerVO> list=adminService.responseSellerView();
		return list;
	}
	
	// 전체 회원리스트 불러오기
	@RequestMapping("/getAllConsumerList.do")
	public ModelAndView getAllConsumerList(){
		List<ConsumerVO> consumerList=adminService.getAllConsumerList();
		return new ModelAndView("getAllConsumerList.admin","consumerList",consumerList);
	}
	
	// 다수의 회원 삭제하기
	@RequestMapping("/deleteConsumers.do")
	@ResponseBody
	public List<ConsumerVO> deleteConsumers(String[] selectConsumer) throws Exception{
		for(int i=0; i<selectConsumer.length; i++){
			ConsumerVO cvo=new ConsumerVO();
			cvo.setId(selectConsumer[i]);
			SellerVO svo=new SellerVO();
			svo.setSellerId(selectConsumer[i]);
			consumerService.deleteConsumer(cvo);
			List<ProductVO> list=sellerService.getProductList(svo);
			for(int j=0; j<list.size(); j++){
				String id=Integer.toString(list.get(j).getProductId());
				sellerService.delelteProduct(id);
			}
			sellerService.deleteSeller(svo);
			questionBoardService.deleteReplyQuestion(cvo);
		}
		List<ConsumerVO> consumerList=adminService.getAllConsumerList();
		return consumerList;
	}
	// 회원 한명 삭제하기
	@RequestMapping("/deleteConsumer.do")
	public ModelAndView deleteConsumer(String id) throws Exception{
		ConsumerVO cvo=new ConsumerVO();
		cvo.setId(id);
		SellerVO svo=new SellerVO();
		svo.setSellerId(id);
		consumerService.deleteConsumer(cvo);
		List<ProductVO> list=sellerService.getProductList(svo);
		for(int i=0;i<list.size();i++){
			String getProductId=Integer.toString(list.get(i).getProductId());
			sellerService.delelteProduct(getProductId);
		}
		sellerService.deleteSeller(svo);
		questionBoardService.deleteReplyQuestion(cvo);
		List<ConsumerVO> consumerList=adminService.getAllConsumerList();
		return new ModelAndView("getAllConsumerList.admin","consumerList",consumerList);
	}
	// 회원 정보 상세보기
	@RequestMapping("/consumerContentView.do")
	public ModelAndView consumerContentView(String id){
		ConsumerVO cvo=consumerService.findConsumerById(id);
		ModelAndView mv=new ModelAndView("consumerContentView.admin","cvo",cvo);
		SellerVO svo=adminService.findSellerByConsumerId(id);
		if(svo!=null){
			mv.addObject("svo",svo);
		}
		return mv;
	}

	// 전체 판매자 리스트 불러오기
	@RequestMapping("/getAllSellerList.do")
	public ModelAndView getAllSellerList(){
		List<SellerVO> sellerList=adminService.getAllSellerList();
		return new ModelAndView("getAllSellerList.admin","sellerList",sellerList);
	}
	// 다수의 판매자 해제하기
	@RequestMapping("/cancelSellers.do")
	@ResponseBody
	public List<SellerVO> cancelSellers(String[] selectSeller) throws Exception{
		for(int i=0; i<selectSeller.length; i++){;
			SellerVO svo=new SellerVO();
			svo.setSellerId(selectSeller[i]);
			List<ProductVO> list=sellerService.getProductList(svo);
			for(int j=0; j<list.size(); j++){
				String id=Integer.toString(list.get(j).getProductId());
				sellerService.delelteProduct(id);
			}
			sellerService.deleteSeller(svo);
			questionBoardService.deleteSellerQuestionBoard(svo);
		}
		List<SellerVO> sellerList=adminService.getAllSellerList();
		return sellerList;
	}
// 판매자 한명 해제하기
	@RequestMapping("/cancelSeller.do")
	public ModelAndView cancelSeller(String sellerId) throws Exception{
		SellerVO svo=new SellerVO();
		svo.setSellerId(sellerId);
		List<ProductVO> pList=sellerService.getProductList(svo);
		for(int i=0;i<pList.size();i++){
			String id=Integer.toString(pList.get(i).getProductId());
			sellerService.delelteProduct(id);
		}
		sellerService.deleteSeller(svo);
		List<ProductVO> list=sellerService.getProductList(svo);
		for(int i=0; i<list.size(); i++){
			String id=Integer.toString(list.get(i).getProductId());
			sellerService.delelteProduct(id);
		}
		questionBoardService.deleteSellerQuestionBoard(svo);
		List<SellerVO> sellerList=adminService.getAllSellerList();
		return new ModelAndView("getAllSellerList.admin","sellerList",sellerList);
	}
	// 판매자 정보 상세보기
	@RequestMapping("/sellerContentView.do")
	public ModelAndView sellerContentView(String sellerId){
		ConsumerVO cvo=consumerService.findConsumerById(sellerId);
		SellerVO svo=adminService.findSellerByConsumerId(sellerId);
		ModelAndView mv=new ModelAndView("sellerContentView.admin","cvo",cvo);
		mv.addObject("svo",svo);
		return mv;
	}
	//판매량 통계 페이지
	@RequestMapping("/salesVolumeStatisticsView.do")
	public ModelAndView salesVolumeStatisticsView() throws Exception{
		List<SellerVO> sellerList=adminService.getAllSellerList();
		for(int i=0; i<sellerList.size(); i++){
			String sellerId=sellerList.get(i).getSellerId();
			SellerVO svo=adminService.salesVolumeStatisticsView(sellerId);
			if(svo!=null){
				sellerList.get(i).setTotalVolume(svo.getTotalVolume());
				sellerList.get(i).setTotalPrice(svo.getTotalPrice());	
			}else{
				sellerList.get(i).setTotalVolume("0");
				sellerList.get(i).setTotalPrice("0");
			}
		}
		return new ModelAndView("salesVolumeStatisticsView.admin","sellerList",sellerList);
	}
	//개인 판매량 통계 페이지(총 상품 판매량)
	@RequestMapping("/salesVolumeContentView.do")
	public ModelAndView salesVolumeContentView(String sellerId) throws Exception{
		List<ProductVO> list=adminService.salesVolumeContentView(sellerId);
		return new ModelAndView("salesVolumeContentView.admin", "list", list);
	}
	//개인 판매량 통계 페이지(총 상품 판매수익)
	@RequestMapping("/salesPriceContentView.do")
	@ResponseBody
	public List<ProductVO> salesPriceContentView(String sellerId) throws Exception{
		List<ProductVO> list=adminService.salesPriceContentView(sellerId);
		return list;
	}
	
	/**
	 * 전체 상품을 정렬해주는 메서드
	 */
	@RequestMapping("/adminGetProductList.do")
	public ModelAndView adminGetProductList(){
		List<ProductVO> list=adminService.adminGetProductList();
		return new ModelAndView("adminGetProductList.admin","list",list);
	}
	@RequestMapping("/adminGetSalesVolumeList.do")
	@ResponseBody
	public List<ProductVO> adminGetSalesVolumeList(){
		List<ProductVO> list=adminService.adminGetSalesVolumeList();
		return list;
	}
	@RequestMapping("/adminGetSalesProfitList.do")
	@ResponseBody
	public List<ProductVO> adminGetSalesProfitList(){
		List<ProductVO> list=adminService.adminGetSalesProfitList();
		return list;
	}
	@RequestMapping("/findProductName.do")
	@ResponseBody
	public List<ProductVO> findProductName(String productName){
		System.out.println("productName:"+productName);
		List<ProductVO> list=adminService.findProductName(productName);
		return list;
	}
	
	@RequestMapping("/getQuestionBoard.do")
	public ModelAndView getQuestionBoard(){
		List<QuestionBoardVO> qvo= adminService.getQuestionBoard();
		return new ModelAndView("getQuestionBoard.admin","qvo",qvo);
	}
	
	@RequestMapping("/deleteBoard.do")
	@ResponseBody
	public List<QuestionBoardVO>deleteBoard(int no) throws SQLException{
		questionBoardService.deleteQuestionBoard(no);
		List<QuestionBoardVO> list= adminService.getQuestionBoard();
		return list;
	}
	
}